﻿namespace SS.WPF.Framework.Selection
{
    #region

    using System.Collections;
    using System.Linq;

    #endregion

    /// <summary>
    /// The class implement interface IStructuredSelection that is used to manage selected item
    /// </summary>
    public class StructureSelection : IStructuredSelection
    {
        #region Constants and Fields

        /// <summary>
        /// The selected.
        /// </summary>
        protected object[] Selected;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StructureSelection"/> class.
        /// </summary>
        protected StructureSelection()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StructureSelection"/> class.
        /// </summary>
        /// <param name="objs">
        /// The objs.
        /// </param>
        public StructureSelection(params object[] objs)
        {
            this.Selected = objs;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether IsEmpty.
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                return this.Selected == null || this.Selected.Length == 0;
            }
        }

        #endregion

        #region Implemented Interfaces

        #region IEnumerable

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.Selected.GetEnumerator();
        }

        #endregion

        #region IStructuredSelection

        /// <summary>
        /// The get first.
        /// </summary>
        /// <returns>
        /// The get first.
        /// </returns>
        public object GetFirst()
        {
            if (this.IsEmpty)
            {
                return null;
            }

            return this.Selected[0];
        }

        /// <summary>
        /// The get first.
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public T GetFirst<T>()
        {
            var first = this.GetFirst();
            if (first is T)
            {
                return (T)first;
            }
            return default(T);
        }

        #endregion

        #endregion
    }

    /// <summary>
    /// The generic class implement interface IStructuredSelection that is used to manage selected item
    /// </summary>
    /// <typeparam name="T">
    /// Type.
    /// </typeparam>
    public class StructureSelection<T> : StructureSelection, IStructuredSelection<T>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StructureSelection{T}"/> class.
        /// </summary>
        /// <param name="objs">
        /// The objs.
        /// </param>
        public StructureSelection(params T[] objs)
        {
            this.Selected = objs != null ? objs.Cast<object>().ToArray() : null;
        }
    }
}